<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@include file="../base.jsp" %> 

<sys:genericTag4o id="menuTree4RoleConfJson" methodName="menuTree4RoleConfJson" className="com.tmpt.controller.taglib.SecurityV2Tag"/>

<sys:genericTag4o id="roleDtoList" methodName="queryA2RoleList" className="com.tmpt.controller.taglib.SecurityV2Tag"/>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>    
<link href="../css/main.css" rel="stylesheet" type="text/css" >
<link rel="stylesheet" type="text/css" href="../../js/jquery/easyui1.5/themes/bootstrap/easyui.css">
<link rel="stylesheet" type="text/css" href="../../js/jquery/easyui1.5/themes/icon.css">
<script type="text/javascript" src="../../js/jquery-1.8.0.min.js"></script>
<script src="../../js/jquery/easyui1.5/jquery.easyui.min.js"></script>
<script src="../../js/jquery/easyui1.5/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript">
//dialog buttons
var jsonParam_buttons=[{
	text:'提交',
	iconCls:'icon-save',
	handler:function(){
		getChecked();
		addRole();
	}
},{
	text:'关闭',
	iconCls:'icon-cancel',
	handler:function(){
		window.parent.closeDialog();
	}
}];

var roleJsonArray;
var roleId;
var roleName;

$(function(){

	//add buttons to dialog
	window.parent.addButtons2IframeDialog(jsonParam_buttons);

	//row data
	var _parameter1_iframedialog = '${paramValues.parameter1_iframedialog[0]}';
	_parameter1_iframedialog = decodeURI(_parameter1_iframedialog);
	_parameter1_iframedialog = decodeURI(_parameter1_iframedialog);
	_parameter1_iframedialog = JSON.parse(_parameter1_iframedialog);

	/*var _parameter2_iframedialog = ${paramValues.parameter2_iframedialog[0]};
	 var _parameter3_iframedialog = ${paramValues.parameter3_iframedialog[0]};*/

	$('#tt1').treegrid({ 
		data:${menuTree4RoleConfJson},
		checkbox: true,
		rownumbers: true,
		idField: 'id',
		treeField:'text', 
		columns:[[ 
		{title:'ID',field:'id',width:30}, 
		{title:'菜单名称',field:'text',width:250}, 
		{field:'opt',title:'菜单权限',width:250,align:'center',
			formatter:function(value,rowData,rowIndex){
				if(rowData.attributes.level == 2){
					var menu1Id = rowData.attributes.menu1Id;
					var menu2Id = rowData.attributes.menu2Id;
					var radioName = "radio_"+menu1Id+"_"+menu2Id;
					var btn = '';
					btn += "<input type='radio' id='"+radioName+"_0' name='"+radioName+"' value='0' checked='checked' />只读 ";  
					btn += "<input type='radio' id='"+radioName+"_1' name='"+radioName+"' value='1' />读写删 ";
					btn += "<input type='radio' id='"+radioName+"_2' name='"+radioName+"' value='2' />下载";
					return btn;
				}
				return "";
			}
		},
		{field:'opt1',title:'数据访问边界',width:250,align:'center',
			formatter:function(value,rowData,rowIndex){
				var type = rowData.attributes.type;
				var hasDataLimit = rowData.attributes.hasDataLimit;
				//0=menu; 10=leave menu; 20=function 30=function url
				if(type == 10 && hasDataLimit==1){
					var menu1Id = rowData.attributes.menu1Id;
					var menu2Id = rowData.attributes.menu2Id;
					var dataLimitTypeId = "dataListTypeId_"+menu1Id+"_"+menu2Id;
					var btn = '';
					btn += "<select class='easyui-combobox' id='"+dataLimitTypeId+"'>";
					/*btn += "<option value='0'>请选择</option>";*/
					btn += "<option value='0'>仅自己的数据</option>";
					btn += "<option value='10'>岗位下级员工数据</option>";
					btn += "<option value='20'>同部门同岗位以及下级员工数据</option>";
					btn += "<option value='30'>公司下所有员工数据</option>";
					btn += "<option value='40'>所有数据可以访问</option>";
					btn += "</select>";
					return btn;
				}
				return "";
			}
		}
		]],
		onClickCell:function(field,row){
			if(field=='text'){
				var id = row.id
				if(isNaN(id)){
					if(isCheckedNode(id)) {
						$('#tt1').treegrid('uncheckNode', id);
					}else{
						$('#tt1').treegrid('checkNode', id);
					}
				}else{
					$(this).treegrid('toggle', id);
				}
			}
		},
		onLoadSuccess:function(row, data){
			$('.easyui-combobox').combobox({editable:false});
		}
	});

	//load data for Menu tree
	roleId = _parameter1_iframedialog.id;
	roleName = _parameter1_iframedialog.rolename;
	loadMenus4RoleConfByRoleId(roleId);


	$("#addRoleId").click(function(){
		getChecked();
		addRole();
	});
});

/**
* load data for Menu tree
* @param roleId
 */
function loadMenus4RoleConfByRoleId(roleId){
	$.ajax({
		type:"POST",
		url:'securityv2Ajax_queryMenus4RoleConfByRoleId.action',
		dataType:"json",
		data:{'roleId':roleId},
		success:function(msg){
			var jarray = jQuery.parseJSON(msg);
			for(var i=0;i<jarray.length;i++){
				var menu2Id4Tree = jarray[i].menu2Id4Tree;
				var accessTypeLevel = jarray[i].accessTypeLevel;
				var dataLimitType = jarray[i].dataLimitType;
				var menu1Id = jarray[i].menu1Id;
				var menu2Id = jarray[i].menu2Id;
				var dataLimitTypeId = "dataListTypeId_"+menu1Id+"_"+menu2Id;
				if($("#"+dataLimitTypeId)[0]){
					$("#"+dataLimitTypeId).combobox('setValue',dataLimitType);
				}
				$('#tt1').treegrid('checkNode',menu2Id4Tree);
				jQuery("input[type='radio'][name='radio_"+menu2Id4Tree+"'][value='"+accessTypeLevel+"']").attr("checked", "checked");
			}
		},
		error:function(er){
			alert(er.responseText.trim());
			if(er.responseText!='' && er.responseText.indexOf('date error')!=-1){
				alert(er.responseText);
			}else{
				if(er.responseText!='' && er.responseText.indexOf('资源权限未分配')!=-1){
					alert('资源权限未分配');
				}
				if(er.responseText!='' && er.responseText.indexOf('登录')!=-1){
					alert('会话超时，请重新登录');
				}else{
					alert(er.responseText.trim());
				}
			}
		}
	});
}



function addRole(){  
    $.ajax({
		type:"POST",
		url:'securityv2Ajax_addRole.action',
		dataType:"json",
		data:{'roleJsonArray':roleJsonArray,'roleName':roleName},
		success:function(msg){
			window.parent.messager_show(msg,2000);
			window.parent.datagrid_fresh();
			window.parent.closeDialog();
		},
		error:function(er){
			alert(er.responseText.trim());
			if(er.responseText!='' && er.responseText.indexOf('date error')!=-1){
				alert(er.responseText);
			}else{
				if(er.responseText!='' && er.responseText.indexOf('资源权限未分配')!=-1){
					alert('资源权限未分配');
				}
				if(er.responseText!='' && er.responseText.indexOf('登录')!=-1){
					alert('会话超时，请重新登录');
				}else{
					alert(er.responseText.trim());
				}
			}
		}
	});
}

/**
* 判断指定ID的node是否是checked状态
* @param id
* @returns {boolean}
 */
function isCheckedNode(id){
	var nodes = $('#tt1').treegrid('getCheckedNodes');
	for(var i=0; i<nodes.length; i++){
		var node = nodes[i];
		var id1 = node.id;
		if(id1==id){
			return true;
		}
	}
	return false;
}

function getChecked(){
	var nodes = $('#tt1').treegrid('getCheckedNodes');//tree getChecked
	var jsonArray = new Array();//创建一个数组
	for(var i=0; i<nodes.length; i++){
		var node = nodes[i];
		var level = node.attributes.level;
		if(level == 2){
			var menu1Id  = node.attributes.menu1Id;
			var menu2Id  = node.attributes.menu2Id;
			var radioName = "radio_"+menu1Id+"_"+menu2Id;
			var accessTypeLevel =  $("input[name='"+radioName+"']:checked").val();
			var dataLimitTypeId = "dataListTypeId_"+menu1Id+"_"+menu2Id;
			var dataLimitType = 0;
			if($("#"+dataLimitTypeId)[0]){
				dataLimitType = $("#"+dataLimitTypeId).combobox('getValue');
			}
			var json = {};
			json.menu1Id = menu1Id;
			json.menu2Id = menu2Id;
			json.accessTypeLevel = accessTypeLevel;
			json.dataLimitType = dataLimitType;
			jsonArray.push(json);
		}
	}
	roleJsonArray = JSON.stringify(jsonArray);
}

</script>
</head>
<body class="font14">

<div id="tt1" ></div>

</body>
</html>